package com.lixin.smartclassroom.ui.home.tab3.fragment

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import com.drake.channel.receiveEvent
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.R
import com.lixin.smartclassroom.api.childzhuticourselist
import com.lixin.smartclassroom.api.getmyhuizhanglist
import com.lixin.smartclassroom.api.getzhutitype
import com.lixin.smartclassroom.api.mychildzhktlist
import com.lixin.smartclassroom.api.zhuticourselist
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.common.sourceSetDialog
import com.lixin.smartclassroom.databinding.FragmentTab3ChildBinding
import com.lixin.smartclassroom.ext.child_id
import com.lixin.smartclassroom.ext.user_role
import com.lixin.smartclassroom.ui.home.adapter.classVenueTasks
import com.lixin.smartclassroom.ui.home.adapter.imgList
import com.lixin.smartclassroom.ui.home.adapter.tab3Class
import com.lixin.smartclassroom.ui.home.adapter.tab3recycler
import com.lixin.smartclassroom.ui.home.tab3.activity.SealAct
import com.lixinkeji.base.BaseFragment
import com.lixinkeji.extension.getIntArgument
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.start
import com.lixinkeji.extension.visible

/**
 * Description
 * Author: lwc
 * Date:2023/12/6 14:03
 */
class Tab3ChildFragment : BaseFragment<FragmentTab3ChildBinding>() {
    override fun getViewBinding(
        inflater: LayoutInflater, container: ViewGroup?
    ) = FragmentTab3ChildBinding.inflate(inflater)

    private var listBean: ArrayList<DataListBean>? = null
    private var classListBean: ArrayList<DataListBean>? = null
    private var sealListBean: ArrayList<DataListBean>? = null
    private var type = 0
    private var fenleiid = ""
    override fun iniView() {

        classListBean = ArrayList()
        listBean = ArrayList()
        sealListBean = ArrayList()
        binding.apply {
            type = getIntArgument("type")


            receiveEvent<String>("refreshClassList") {
                getData()
            }

            when (requireContext().user_role) {
                1 -> {
                    llStudent.gone()
                    recycler.classVenueTasks(1).models = listBean
                }

                2 -> {
                    llStudent.visible()
                    recycler.classVenueTasks(1).models = listBean

                }

                3 -> {
                    llStudent.gone()

                    recycler.tab3recycler().apply {
                        R.id.tvSet.onClick {
                            requireContext().sourceSetDialog(
                                "2",
                                models?.get(modelPosition) as DataListBean
                            )
                        }
                    }.models = listBean
                }
            }

            recyclerClass.tab3Class().apply {
                R.id.ll.onClick {
                    fenleiid = (models?.get(modelPosition) as DataListBean).id.toString()
                    tvTypeName.text = (models?.get(modelPosition) as DataListBean).name.toString()
                    getData()
                    when (requireContext().user_role) {
                        2 -> getSealData()
                    }
                }
            }.models = classListBean

            getClassData()


        }
    }

    /**
     *@Description 获取我的集章数据
     *@date 创建时间2024/3/13
     *
     */
    private fun getSealData() {
        scopeNetLife {
            sealListBean?.clear()
            sealListBean?.addAll(getmyhuizhanglist(fenleiid)!!)
            binding.recyclerSeal.imgList(1).models = sealListBean
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun getClassData() {
        scopeNetLife {
            classListBean?.addAll(getzhutitype()!!)
            binding.recyclerClass.adapter?.notifyDataSetChanged()

            fenleiid = classListBean?.get(0)!!.id.toString()
            binding.tvTypeName.text = classListBean?.get(0)!!.name.toString()
            getData()
            when (requireContext().user_role) {
                2 -> getSealData()
            }
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun getData() {
        scopeNetLife {
            listBean?.clear()
            when (requireContext().user_role) {
                1 -> {
                    if (requireContext().child_id.isEmpty()) {
                        listBean = ArrayList()
                    } else {
                        listBean?.addAll(
                            childzhuticourselist(
                                1,
                                1000,
                                requireContext().child_id,
                                fenleiid
                            )!!
                        )
                    }
                }

                else -> listBean?.addAll(zhuticourselist(1, 1000, type.toString(), fenleiid)!!)
            }

            binding.recycler.adapter?.notifyDataSetChanged()
        }
    }

    override fun onResume() {
        super.onResume()


    }

    override fun onClick() {
        binding.tvDownload.onClick {
            start(SealAct::class.java)
        }
    }


    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) {

            if (!fenleiid.isNullOrEmpty()) {
                getData()
                when (requireContext().user_role) {
                    2 -> {
                        getSealData()
                    }
                }
            }

        }
    }
}